package start.demo01.datatype.json.entity.editor.jsonEditor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import start.demo01.datatype.json.interfaces.JSONEditor;

import java.text.SimpleDateFormat;
import java.util.List;

import static start.demo01.datatype.json.constant.Constant.$;
import static start.demo01.datatype.json.constant.Constant.TIMETYPE;


//增加对象属性
public class JSONInsertEditor extends JSONEditor {

    private List<String[]> list;
    //Object insert({变量名,类型},{name,$var})
    public JSONInsertEditor(String args) {
        super(args);
        list = deal();
    }
    {
        name="INSERT";
    }
    @Override
    protected void operationObject(JSONObject object, String key) {
        return;
    }

    @Override
    protected void operationArray(JSONArray array, int index) {
        return;
    }

    @Override
    protected void operation(JSONObject object) {
        list.forEach(arr->{
            if(TIMETYPE.equals(arr[1])) {//时间戳
                object.put(arr[0],System.currentTimeMillis());
            } else if (arr[1].startsWith($)) {//变量
                object.put(arr[0],map.get(arr[1].substring(1)));
            } else{//yyyyMMdd
                SimpleDateFormat sdf=new SimpleDateFormat(arr[1]);
                object.put(arr[0],sdf.format(System.currentTimeMillis()));
            }
        });
    }
}
